<?php
/**
 * 毛文科
 */
namespace app\admin\builder\bind;
class Table
{
    protected $toolbardemo = [];
    protected $over = [
        'page'=>false,
        'totalRow'=>0
    ];
    protected $column = [];
    protected $button = [];
    protected $monitoraction =[];

    /**
     * @param null设置cid $cid
     * @return $this
     */
    public function setCid($cid=null){
        $this->over['cid'] = $cid;
        return $this;
    }


    /**设置监听接口
     * @param $type
     * @param string $action
     * @return $this
     */
    public function setMonitorAction($type='switch',$action=''){
        $action = url($action);
        $this->monitoraction[$type] = $action;
        return $this;
    }

    /**设置modelid
     * @param null $modelid
     * @return $this
     */
    public function setModelId($modelid=null){
        $this->over['model_id'] = $modelid;
        return $this;
    }

    /**设置获取数据的地址
     * @param null $action
     * @return $this
     */
    public function setAction($action=null){
        $action = url($action);
        $this->over['action'] = $action;
        return $this;
    }

    /**开启分页
     * @param array $layout
     * @return $this
     */
    public function setPage($status=true){
        $this->over['page'] = $status;
        return $this;
    }

    /**设置当前操作
     * @param string $desc
     * @return $this
     */
    public function setRightHeader($desc='操作',$width=250,$class='',$style=''){
        $data['desc'] = translateStr($desc);
        $data['width'] = $width;
        $data['class'] = $class;
        $data['style'] = $style;
        $this->over['rightheader'] = $data;
        return $this;
    }

    /**设置按钮
     * @param string类型 $type
     * @param string跳转地址 $action
     * @param string样 $class
     * @param string样式 $style
     * @return $this
     */
    public function setButton($type='button',$desc='',$action='',$class='',$style=''){
        $data['type'] = $type;
        $data['action'] = url($action);
        $data['class'] = $class;
        $data['style'] = $style;
        $data['desc'] = translateStr($desc);
        $this->button[] = $data;
        return $this;
    }

    /**添加列
     * @param 类型 $type
     * @param 字段名 $name
     * @param 头部名称 string $desc
     * @param 有动作的api接口 string $action
     * @param string宽度 $width
     * @param string样式 $style
     * @param array数组 $option
     * @return $this
     */
    public function setColumn($type,$name,$desc='',$action='',$width=null,$align='center',$style='',$option=['开启','关闭'],$totalRow=false){
        $data['type'] = $type;
        $data['name'] = $name;
        $data['desc'] = translateStr($desc);
        $data['width'] = $width;
        $data['style'] = $style;
        $data['option'] = $option;
        $data['totalRow'] = $totalRow;
        if($totalRow){
            $this->over['totalRow'] = 1;
        }
        if(!$align){
            $align ='center';
        }
        $data['align'] = $align;
        if($action){
            $action = url($action);
        }
        $data['action'] = $action;
        $this->column[] = $data;
        return $this;
    }

    /**设置监听条
     * @param string监听条类型 $type
     * @param string监听条内容 $desc
     * @param null监听条api $action
     * @param null样式 $class
     * @param null样式 $style
     * @return $this
     */
    public function addToolbarDemo($type='text',$desc='',$option=[],$action=null,$class=null,$style=null){
        $data['type'] = $type;
        $data['desc'] = translateStr($desc);
        if($action){
            $action = url($action);
        }
        $data['action'] = $action;
        $data['class'] = $class;
        $data['style'] = $style;
        $data['option'] = $option;
        $this->toolbardemo[] = $data;
        return $this;
    }
    public function fetch($table = 'table'){
//        echo '<pre>';
//        print_r($this->button);
//        exit;
        if(!$this->monitoraction){
            $this->setMonitorAction('switch','document/change');
        }
        return view('index/'.$table,['toolbardemo'=>$this->toolbardemo,'over'=>$this->over,'column'=>$this->column,'button'=>$this->button,'buttons'=>json_encode($this->button),'monitoraction'=>$this->monitoraction]);
    }

}